﻿package cn.hadron.aa.dao.impl;
import java.util.List;

import org.springframework.stereotype.Repository;

import cn.hadron.aa.dao.*;
import cn.hadron.aa.entity.*;
import cn.hadron.aa.util.Pager;
@Repository("articleDao")
public class ArticleDaoImpl extends BaseDaoImpl<Article> implements ArticleDao{
	//构造器，初始化实体类型
	public ArticleDaoImpl() {
		super(Article.class);
	}
    public Integer getCount(int channelId) {
    	String hql="select count(*) from Article where channel.id=?";
    	Number m=(Number)getSession().createQuery(hql)
    			.setParameter(0, channelId).uniqueResult();
    	return m.intValue();
    }
	@Override
	public  Pager<Article> findPager(int channelId,int pageNo,int pageSize) {
		getSession().clear();
		String hql="from Article where channel.id=? order by id asc";
		final Pager<Article> pager = new Pager<Article>(pageNo, pageSize, this.getCount(channelId));
        List<Article> list = getSession().createQuery(hql,Article.class)
        		.setParameter(0, channelId)
        		.setFirstResult((pager.getPageNo()-1)*pageSize)
        		.setMaxResults(pageSize).list();
		pager.setData(list);
		return pager;
	}
	
}